﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Infrastructure;
using Microsoft.Extensions.Options;
using OpenAuth.Repository.Domain;

namespace OpenAuth.App
{
    public class PayToolsApp
    {
        MySocket mySocket;
        private IOptions<BankPlus> bankPlus;      
        string HOST, CUST_ID, PASSWORD, USER_ID, LANGUAGE;
        int PORT;
        //string url = HttpContext.Current.Server.MapPath("~/Configs/JH.xml");
        //string url = HttpRuntime.AppDomainAppPath + "Configs\\JH.xml";
        public PayToolsApp(IOptions<BankPlus> _bankPlus)
        {
            PORT =Int32.Parse(_bankPlus.Value.PORT);
            HOST = _bankPlus.Value.HOST;
            CUST_ID = _bankPlus.Value.CUST_ID;
            PASSWORD = _bankPlus.Value.PASSWORD;
            USER_ID = _bankPlus.Value.USER_ID;
            LANGUAGE = _bankPlus.Value.LANGUAGE;
            mySocket = new MySocket(PORT, HOST);
        }
        /// <summary>
        /// 建行支付流水查询
        /// </summary>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="starthour">开始小时</param>
        /// <param name="startminute">开始分钟</param>
        /// <param name="endhour">结束小时</param>
        /// <param name="endminute">结束分钟</param>
        /// <returns></returns>
        public string GetPayOrderList(string startdate, string enddate, string starthour = "00", string startminute = "00", string endhour = "23", string endminute = "59")
        {
            string xlh = DateTime.Now.ToString("yyyyMMddHHmmssff");
            string str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> "
+ "<TX>"
  + "<REQUEST_SN>" + xlh + "</REQUEST_SN>"
  + "<CUST_ID>" + CUST_ID + "</CUST_ID>"
  + "<USER_ID>" + USER_ID + "</USER_ID>"
  + "<PASSWORD>" + PASSWORD + "</PASSWORD>"
  + "<TX_CODE>5W1002</TX_CODE>"
  + "<LANGUAGE>" + LANGUAGE + "</LANGUAGE>"
  + "<TX_INFO>"
   + "<START>" + startdate + "</START>"
   + "<STARTHOUR>" + starthour + "</STARTHOUR>"
   + "<STARTMIN>" + startminute + "</STARTMIN>"
   + "<END>" + enddate + "</END>"
   + "<ENDHOUR>" + endhour + "</ENDHOUR>"
   + "<ENDMIN>" + endminute + "</ENDMIN>"
   + "<KIND>1</KIND>"
   + "<ORDER></ORDER>"
   + "<ACCOUNT></ACCOUNT>"
   + "<DEXCEL>1</DEXCEL>"
   + "<MONEY></MONEY>"
   + "<NORDERBY>2</NORDERBY>"
   + "<PAGE>1</PAGE>"
   + "<POS_CODE></POS_CODE>"
   + "<STATUS>3</STATUS>"
   + "<Mrch_No></Mrch_No>"
   + "</TX_INFO>"
    + "</TX>";
            return mySocket.SendMessage(str);
        }
        /// <summary>
        /// 建行退款流水查询
        /// </summary>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="starthour">开始小时</param>
        /// <param name="startminute">开始分钟</param>
        /// <param name="endhour">结束小时</param>
        /// <param name="endminute">结束分钟</param>
        public string GetRefundOrderList(string startdate, string enddate, string starthour = "00", string startminute = "00", string endhour = "23", string endminute = "59")
        {
            string xlh = DateTime.Now.ToString("yyyyMMddHHmmssff");
            string str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> "
+ "<TX>"
  + "<REQUEST_SN>" + xlh + "</REQUEST_SN>"
  + "<CUST_ID>" + CUST_ID + "</CUST_ID>"
  + "<USER_ID>" + USER_ID + "</USER_ID>"
  + "<PASSWORD>" + PASSWORD + "</PASSWORD>"
  + "<TX_CODE>5W1003</TX_CODE>"
  + "<LANGUAGE>" + LANGUAGE + "</LANGUAGE>"
  + "<TX_INFO>"
   + "<START>" + startdate + "</START>"
   + "<STARTHOUR>" + starthour + "</STARTHOUR>"
   + "<STARTMIN>" + startminute + "</STARTMIN>"
   + "<END>" + enddate + "</END>"
   + "<ENDHOUR>" + endhour + "</ENDHOUR>"
   + "<ENDMIN>" + endminute + "</ENDMIN>"
   + "<KIND>1</KIND>"
   + "<ORDER></ORDER>"
   + "<ACCOUNT></ACCOUNT>"
   + "<MONEY></MONEY>"
   + "<NORDERBY>2</NORDERBY>"
   + "<PAGE>1</PAGE>"
   + "<POS_CODE></POS_CODE>"
   + "<STATUS>3</STATUS>"
   + "<Mrch_No></Mrch_No>"
   + "</TX_INFO>"
    + "</TX>";
            return mySocket.SendMessage(str);
        }
        /// <summary>
        /// 获取订单支付状态
        /// </summary>
        /// <param name="OrderNum"></param>
        /// <returns></returns>
        public string GetOrder(string OrderNum)
        {
            string xlh = DateTime.Now.ToString("yyyyMMddHHmmssff");
            string str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> "
  + "<TX>"
  + "<REQUEST_SN>" + xlh + "</REQUEST_SN>"
  + "<CUST_ID>" + CUST_ID + "</CUST_ID>"
  + "<USER_ID>" + USER_ID + "</USER_ID>"
  + "<PASSWORD>" + PASSWORD + "</PASSWORD>"
  + "<TX_CODE>5W1002</TX_CODE>"
  + "<LANGUAGE>" + LANGUAGE + "</LANGUAGE>"
  + "<TX_INFO>"
   + "<START></START>"
   + "<STARTHOUR></STARTHOUR>"
   + "<STARTMIN></STARTMIN>"
   + "<END></END>"
   + "<ENDHOUR></ENDHOUR>"
   + "<ENDMIN></ENDMIN>"
   + "<KIND>1</KIND>"
   + "<ORDER>" + OrderNum + "</ORDER>"
   + "<ACCOUNT></ACCOUNT>"
   + "<DEXCEL>1</DEXCEL>"
   + "<MONEY></MONEY>"
   + "<NORDERBY>2</NORDERBY>"
   + "<PAGE>1</PAGE>"
   + "<POS_CODE></POS_CODE>"
   + "<STATUS>3</STATUS>"
   + "<Mrch_No></Mrch_No>"
   + "</TX_INFO>"
    + "</TX>";
            return mySocket.SendMessage(str);
        }
        /// <summary>
        /// 退款操作
        /// </summary>
        /// <param name="money">退款金额</param>
        /// <param name="orderNumber">退款订单号</param>
        /// <returns></returns>
        public string Refund(double money, string orderNumber)
        {
            string xlh = DateTime.Now.ToString("yyyyMMddHHmmssff");
            string str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> "
   + "<TX>"
   + "<REQUEST_SN>" + xlh + "</REQUEST_SN>"
   + "<CUST_ID>" + CUST_ID + "</CUST_ID>"
   + "<USER_ID>" + USER_ID + "</USER_ID>"
   + "<PASSWORD>" + PASSWORD + "</PASSWORD>"
   + "<TX_CODE>5W1004</TX_CODE>"
   + "<LANGUAGE>" + LANGUAGE + "</LANGUAGE>"
   + "<TX_INFO>"
   + "<MONEY>" + money + "</MONEY>"
   + "<ORDER>" + orderNumber + "</ORDER>"
    + "<REFUND_CODE></REFUND_CODE>"
    + "</TX_INFO>"
   + "<SIGN_INFO></SIGN_INFO>"
  + "<SIGNCERT></SIGNCERT>"
     + "</TX>";
            return mySocket.SendMessage(str);
        }
    }
}
